﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PaymentHelper.Core.RelatedObject;
using PaymentHelper.Core.Extension;
using PaymentHelper.Core.ExceptionHandler;
using System.ComponentModel.DataAnnotations;

namespace PaymentHelper.Methods.PaypalStandard.Models
{
    public class ConfigModel:PaymentHelperModel
    {
        public bool UseSandBox { get; set; }
        public bool PassDetailListItems { get; set; }
        public bool EnableUTF8Encoding { get; set; }
        public bool EnableIPN { get; set; }
        public bool OverrideAddress { get; set; }
        
        [Range(0,1000,ErrorMessage="Handling fee must be between 0 and 1000")]
        [Required(ErrorMessage="Handling fee is required")]
        public decimal HandlingFee { get; set; }

        [Required(ErrorMessage = "BusinessEmail is required")]
        public string BusinessEmail { get; set; }

        public override void InitModel(IList<PHSettingItem> paypalStandardSettings)
        {
            if (paypalStandardSettings == null || paypalStandardSettings.Count == 0)
                throw new PaymentHelperException(ExceptionLevel.Warning, "ConfigModel-InitModel", "List of settings is empty. Loading specified setting failed !");

            UseSandBox = GetValueConfig("UseSandBox", paypalStandardSettings);
            PassDetailListItems = GetValueConfig("PassDetailListItems", paypalStandardSettings);
            EnableUTF8Encoding = GetValueConfig("EnableUTF8Encoding", paypalStandardSettings);
            EnableIPN = GetValueConfig("EnableIPN", paypalStandardSettings);
            OverrideAddress = GetValueConfig("OverrideAddress", paypalStandardSettings);
            HandlingFee = GetValueConfig("HandlingFee", paypalStandardSettings);
            BusinessEmail = GetValueConfig("BusinessEmail", paypalStandardSettings);
        }

        public override IList<PHSettingItem> ToListAttribute()
        {
            var listAtrr = new List<PHSettingItem>();

            listAtrr.Add(new PHSettingItem() {
                DisplayName = "Use SandBox",
                Name = "UseSandBox",
                TypeSetting = UseSandBox.GetType(),
                ValueInStr = UseSandBox.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Pass Detail ListItems",
                Name = "PassDetailListItems",
                TypeSetting = PassDetailListItems.GetType(),
                ValueInStr = PassDetailListItems.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Enable UTF8 Encoding",
                Name = "EnableUTF8Encoding",
                TypeSetting = EnableUTF8Encoding.GetType(),
                ValueInStr = EnableUTF8Encoding.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Override Address",
                Name = "OverrideAddress",
                TypeSetting = OverrideAddress.GetType(),
                ValueInStr = OverrideAddress.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Enable IPN",
                Name = "EnableIPN",
                TypeSetting = EnableIPN.GetType(),
                ValueInStr = EnableIPN.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Handling Fee",
                Name = "HandlingFee",
                TypeSetting = HandlingFee.GetType(),
                ValueInStr = HandlingFee.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Business Email",
                Name = "BusinessEmail",
                TypeSetting = BusinessEmail.GetType(),
                ValueInStr = BusinessEmail.ToString()
            });

            return listAtrr;
        }
    }
}
